package com.ahwei.shop.auth.security.handler;

import com.ahwei.r.R;
import com.ahwei.r.ResultCode;
import com.ahwei.shop.auth.util.ResponseUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author：ahWei
 * @email: ahwei163@qq.com
 * @date: 2022-05-19 17:20:17
 * @description: ExceptionTranslationFilter会使用AuthenticationEntryPoint启动身份验证方案。！
 */
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Resource
    private ResponseUtils responseUtils;
    @Override
    public void commence(HttpServletRequest req, HttpServletResponse resp, AuthenticationException authException) throws IOException, ServletException {
        //返回错误消息401 表示要求客户端认证
        responseUtils.outWriter(R.error(ResultCode.LOGIN_AUTH,"用户未登录或Token超期，请重新登录?"), HttpStatus.UNAUTHORIZED.value(),resp);
    }
}